package com.why.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.servicebase.exception.GuilException;
import com.why.serviceedu.bean.EduCourse;
import com.why.serviceedu.bean.EduCourseDescription;
import com.why.serviceedu.bean.FrontVo.CourseQueryVo;
import com.why.serviceedu.bean.Vo.courseInfoVo;
import com.why.serviceedu.mapper.EduCourseMapper;
import com.why.serviceedu.service.EduCourseDescriptionService;
import com.why.serviceedu.service.EduCourseService;
import com.why.serviceedu.bean.FrontVo.CourseWebVo;
import com.why.serviceedu.bean.Vo.coursePublishVo;
import com.why.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.why.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author why
 * @since 2021-02-01
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述注入
    @Autowired
    EduCourseDescriptionService service;

    //课程注入
    @Resource
    EduCourseMapper mapper;

    //课程小节章节注入
    @Autowired
    EduChapterService chapterService;
    @Autowired
    EduVideoService videoService;


    @Override
    public String saveVo(courseInfoVo courseVo) {
        //1.向课程表中添加课程信息
        EduCourse course = new EduCourse();
        //将属性赋值给EduCourse
        BeanUtils.copyProperties(courseVo, course);
        //course.setSubjectParentId("1101348944971091969");
        int insert = baseMapper.insert(course);
        if (insert == 0) {
            throw new GuilException(20001, "添加课程失败！");
        }
        System.out.println("课程Id:" + course.getId());
        //得到mp生成的id
        String id = course.getId();
        //2.向课程描述表中添加课程的描述
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseVo.getDescription());
        //设置id完成一对一关联！！！
        description.setId(id);
        boolean save = service.save(description);

        //返回课程对应的课程id
        return id;
    }

    @Override
    public courseInfoVo getCourseVoById(String courseId) {
        courseInfoVo result = new courseInfoVo();
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //将课程信息复制过去
        BeanUtils.copyProperties(eduCourse, result);
        //将课程描述复制过去
        EduCourseDescription eduCourseDescription = service.getById(courseId);
        result.setDescription(eduCourseDescription.getDescription());
        return result;

    }

    @Override
    public void updateCourseById(courseInfoVo courseInfoVo) {
        //修改课程信息
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, course);
        int id = baseMapper.updateById(course);
        if (id <= 0) {
            throw new GuilException(20001, "修改课程信息失败");
        }
        //修改课程描述
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());

        service.updateById(description);
        //修改课程描述信息

    }

    @Override
    public coursePublishVo publishCourseInfo(String courseId) {


        return mapper.getcoursePublishVo(courseId);
    }


    @Override
    public void removeCourseInfoById(String courseId) {
        //根据id删除小节
        videoService.removeVideoByCourseId(courseId);
        //根据id删除章节
        chapterService.removeChapterByCourseId(courseId);

        //根据id删除课程信息
        int i = baseMapper.deleteById(courseId);
        if (i <= 0) {
            throw new GuilException(20001, "课程删除失败！");
        }
        //根据id删除描述
        service.removeDescrpitionById(courseId);
    }


    /**
     * 得到8个热门课程
     *
     * @return
     */

    @Override
    public List<EduCourse> getHotCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");

        return baseMapper.selectList(wrapper);
    }

    /**
     * 根据讲师id查询当前讲师的课程列表
     *
     * @param teacherId
     * @return
     */
    @Override
    public List<EduCourse> selectByTeacherId(String teacherId) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<EduCourse>();
        queryWrapper.eq("teacher_id", teacherId);
//按照最后更新时间倒序排列
        queryWrapper.orderByDesc("gmt_modified");
        List<EduCourse> courses = baseMapper.selectList(queryWrapper);
        return courses;
    }

    /**
     * 分页查询课程列表
     *
     * @param pageParam
     * @param courseQuery
     * @return
     */

    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        //查询的条件
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id",
                    courseQuery.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }
        //根据什么来进行排序
        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }
        baseMapper.selectPage(pageParam, queryWrapper);
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    /**
     * 根据课程id查询课程信息和讲师信息
     * @param courseId
     * @return
     */
    @Override
    public CourseWebVo selectInfoWebById(String courseId) {
       return baseMapper.selectInfoWebById(courseId);

    }

}
